package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 数据库连接工具类，用于获取数据库连接
 */
public class DBUtil {
    private static final String PROPERTY_FILE = "database.properties";
    private static Properties properties;

    static {
        properties = new Properties();
        try {
            properties.load(DBUtil.class.getClassLoader().getResourceAsStream(PROPERTY_FILE));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接
     * @return 数据库连接对象
     */
    public static Connection getConnection() throws SQLException {
        String url = properties.getProperty("jdbc.url");
        String user = properties.getProperty("jdbc.user");
        String password = properties.getProperty("jdbc.password");

        return DriverManager.getConnection(url, user, password);
    }
}